// Main.cpp includes the program enterancy point
//
//  Sorter does that stuff:
//  1. Try to sort file in memory ( if file size not very big )
//		If sorted - success
//      If there are some problems with memory allocation, start sort using the less memory buffer
//      ( go to step 2 )
//  2. If we can't sort in memory - start sort using temporary files
//      Split file to some parts, which can be allocated to memory
//      Sort them and write to files (binary)
//		Merge theese files using buffers
//		If we can't allocate needed size of buffer, reduce that size and try again
//
#include "stdafx.h"

#include <string>
#include <stdlib.h>
#include <Windows.h>
#include <vector>
#include <iostream>
#include <sstream>
#include "Logger.h"
#include "SortStrategy.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	_SYSTEMTIME tmp;
	Logger lg( 8192 , wstring(L"C:\\log.txt"));
	// See are there enought of parametrs
	if( argc < 3 )
	{
		cout << "Invalid format of command line arguments\n";
		lg.WriteErrorInfo(L"Invalid format of command line arguments\n");
		return -1;
	}
	
	wstring strSource( argv[1] );
	wstring strDest( argv[2] );
	wstring strTempPath( L"C:\\" );
	if( strTempPath[ strTempPath.size() - 1] != L'\\' )
			strTempPath.push_back(L'\\');
	GetLocalTime(&tmp);

	// Do sort
	SortStrategy sortier( &lg );
	sortier.SortFile( strSource , strDest , strTempPath );

	_SYSTEMTIME tmp2;
	GetLocalTime(&tmp2 );

	wstringstream ss;
	ss << L"Time : ";
	ss << ((tmp2.wMinute - tmp.wMinute ) * 60 * 1000) + ((tmp2.wSecond - tmp.wSecond ) * 1000) + (tmp2.wMilliseconds - tmp.wMilliseconds );
	lg.WriteTraceInfo( ss.str() );
	lg.WriteTraceInfo(L"WELL DONE");

	cout << "Well done\n";
	return 0;
}

